<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();
    });
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="${ctx}/css/Tree.css"/>
<!-- jQplot CSS END -->

<h1 class="page-title">浏览组织架构</h1>


<div class="container_12 clearfix leading">
    <div class="tabs side grid_4 leading" style="width:500px;">
        <ul class="clearfix" id="left_level"></ul>
        <section>
            <section class="clearfix" style="display: block;">

                <div class="grid_12 alpha">
                    <div id="tree_view"></div>
                </div>
            </section>
        </section>
    </div>
</div>
<div class="clear"></div>

<input type="hidden" id="current_organization_id"/>
<div class="contextMenu" id="OrganizationMenu">
    <ul>
        <li id="opt_delete"><img src="delete.png"/> 删除</li>
    </ul>
</div>

<script type="text/javascript" src="${ctx}/js/Tree.js"></script>
<script type="text/javascript" src="${ctx}/js/jquery.contextmenu.r2.js"></script>
<script type="text/javascript">
    var current_tree_node = null;
    treeview.prototype.onnodeexpand	= function(sender){initMenu();}
    treeview.prototype.onselectchange	= function(before,after){
        if(after != null){
            setCurrentNodeId(after.id);
            current_tree_node = after;
        }
    }

    var mTreeViews;
    $(document).ready(function() {
        //getLeftLevels(null);
        setTimeout("init()", 600);//0.6秒后执行init()
    });

    function init() {
        getLeftLevels(null);

        setTimeout("initMenu()", 600);
    }

    //得到左边的结点
    function getLeftLevels() {
        $.getJSON("${ctx}/ajax/organization-children.xhtml", function(children) {
            if (children != null && children.length != 0) {
                var leftLevelStr = "";
                var firstId = 0;
                $.each(children, function(i, child) {
                    //alert(child.id+":"+child.fullName)
                    if (i == 0) {
                        firstId = child.id;
                        $("#default_organization_id").val(firstId);
                        nodeClick(firstId);
                        leftLevelStr += '<li><a href="#" class="current left_level_a sihai_tree_node" onmousedown="setCurrentNodeId('+child.id+')" onclick="onLeftNodeClick(' + child.id + ', this)">' + child.fullName + '</a></li>';
                    } else {
                        leftLevelStr += '<li><a href="#" class="left_level_a sihai_tree_node" onmousedown="setCurrentNodeId('+child.id+')" onclick="onLeftNodeClick(' + child.id + ', this)">' + child.fullName + '</a></li>';
                    }
                });
                $("#left_level").html("");
                $("#left_level").append(leftLevelStr);
                getChildrenById(firstId);
                setCurrentNodeId(firstId);
                mTreeViews = new Array(children.length);
            }
        });
    }

    function onTabChanage(obj) {
        $(".left_level_a").each(function() {
            //alert($(this).text())
            $(this).removeClass("current");
        });
        $(obj).addClass("current")
    }

    //单击左边的结点
    function onLeftNodeClick(id, obj) {
        nodeClick(id);
        onTabChanage(obj);
        setCurrentNodeId(id);
        
        var tree_view = null;
        for (var i = 0; i < mTreeViews.length; i++) {
            var t = mTreeViews[i];
            if (t != null) {
                var treeId = t.id;
                if (treeId == "treeview" + id) {
                    tree_view = t;
                    break;
                }
            }
        }
        $("#tree_view").html("");
        if (tree_view == null) {
            getChildrenById(id);
        } else {
            tree_view.create(document.getElementById("tree_view"));
        }
        
        setTimeout("initMenu()", 600);
    }

    function getChildrenById(currentNode) {
        var paramsStr = (typeof currentNode == "number") ? "?parentId=" + currentNode : "?parentId=" + currentNode.id
        $.getJSON("${ctx}/ajax/organization-children.xhtml" + paramsStr, function(children) {
            if (children != null && children.length != 0) {
                if (typeof currentNode == "number") {
                    var tree_view = new treeview("treeview" + currentNode, "tree", true, false);//第四个参数是不否可移动节点
                    //var tree_view = new treeview("treeview" + currentNode, "tree", true, true);
                    $.each(children, function(i, child) {
                        var n = new node(child.fullName, child.fullName, "", null, null, null, child.id);
                        tree_view.add(n);
                        tree_view.get
                        getChildrenById(n);
                    });
                    $("#tree_view").html("");
                    tree_view.create(document.getElementById("tree_view"));
                    mTreeViews.push(tree_view);
                } else {
                    $.each(children, function(i, child) {
                        var n = new node(child.fullName, child.fullName, "", null, null, null, child.id);
                        currentNode.add(n);
                        getChildrenById(n);
                    });
                }
            }
        });
    }

    //得到左边当前部门对应的列表(此页面不需要些功能)
    function onNodeClick(sender) {
        nodeClick(sender.id);
    }

    function nodeClick(organizationId) {
    }

    //设置并处理右键菜单
    function initMenu() {
        if(true){
            return;
        }
        $("a.sihai_tree_node").contextMenu("OrganizationMenu", {
            bindings: {
                "opt_edit": function(t) {
                    //alert("编辑id:"+$("#current_organization_id").val());
                },
                "opt_delete": function(t) {
                    //alert("删除id:"+$("#current_organization_id").val());
                    var did = $("#current_organization_id").val();
                    if(did > 0){
                        if(confirm("确定要删除该组织机构吗？")){
                            $.ajax({
                                type: 'POST',
                                url:"${ctx}/organization/remove.xhtml",
                                data:{"organizationId":did},
                                success: function(result){
                                    if(result == true || result == "true"){
                                        if(current_tree_node != null){
                                            current_tree_node.remove();
                                        }
                                    }else{
                                        alert("该组织机构不可删除");
                                        return;
                                    }
                                }
                            });
                        }
                    }
                }
            }

        });
    }

    //处理移动事件
    function onMove(moveId, newParentId){
        if(true){
            return;
        }
        if(moveId > 0 && newParentId > 0){
            $.ajax({
                type: 'POST',
                url:"${ctx}/ajax/organization-move.xhtml",
                data:{"move_id":moveId, "new_parent_id":newParentId},
                success: function(result){
                    if(result == true || result == "true"){
                        location.reload();
                    }
                }
            });
        }
    }

    function setCurrentNodeId(id){
        $("#current_organization_id").val(id);
    }
</script>
